package cn.kgm.lengthOfLongestSubstring;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Author: Kappi
 * @Date: 2023-2-28 16:32
 * @ClassName: Solution
 * @Description: cn.kgm.leetCode Solution
 * @Version 1.8
 */
public class Solution {
    /**
     * 获取无重复字符的最长子串
     *
     * @param param 参数
     * @return
     */
    public int lengthOfLongestSubstring(String param) {
        return Objects.equals("", param) ? 0 : calculate(param);

    }

    public int calculate(String param) {
        int max = 0;
        List<String> sa = Arrays.asList(param.split(""));
        List<String> son = new ArrayList<>();
        for (int i = 0; i < sa.size(); i++) {
            int ind = son.indexOf(sa.get(i));
            if (ind == -1) {
                son.add(sa.get(i));
                int length = son.size();
                max = Math.max(max, length);
            } else {
                if (ind == (son.size() - 1)) {
                    son.clear();
                    son.add(sa.get(i));
                } else {
                    son = son.subList(ind + 1, son.size());
                    son.add(sa.get(i));
                }
            }
        }
        return max;
    }
}
